package com.yummy.controller;

import com.github.pagehelper.PageInfo;
import com.yummy.common.ResultResponse;
import com.yummy.dto.RoleVo;
import com.yummy.entity.Role;
import com.yummy.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("role")
public class RoleController {
    @Autowired
    RoleService roleService;
    @GetMapping("/list")
    public ResultResponse<PageInfo<Role>> rlist(@RequestParam(value = "now",defaultValue = "1") Integer now,
                                                @RequestParam(value = "size",defaultValue = "3")Integer size,
                                                String name){
        ResultResponse<PageInfo<Role>> result = null;

        try {
            PageInfo<Role> rolePageInfo = roleService.queryByPage(now, size, name);
            result = new ResultResponse<>(200,"查询角色成功!",rolePageInfo);
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"查询角色失败!");
        }

        return result ;
    }

    @PutMapping("/modify")
    public ResultResponse<Void> modify(@RequestBody Role role){
        ResultResponse<Void> result = null;
        try {
            //根据 ID 查询出来 当前数据  把前台传递过来的数据  赋值到新查询出来的对象上
            Role r = roleService.queryRoleByKey(role.getId());
            r.setRoleName(role.getRoleName());
            r.setRoleDes(role.getRoleDes());
            roleService.modify(r);
            result = new ResultResponse<>(200,"修改角色成功!");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"修改角色失败!");
        }
        return result ;
    }

    @PostMapping("/add")
    public ResultResponse<Void> add(@RequestBody Role role){
        ResultResponse<Void> result = null;
        try {
            role.setCreateTime(new Date());
            role.setRoleStatus(0);
            role.setIsDelete(0);
            roleService.add(role);
            result = new ResultResponse<>(200,"添加角色成功!");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"添加角色失败!");
        }
        return result ;
    }
    @DeleteMapping("/del/{id}")
    public ResultResponse<Void> del(@PathVariable("id") Integer id){
        ResultResponse<Void> result = null;
        try {
            roleService.removeBykey(id);
            result = new ResultResponse<>(200,"删除角色成功!");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"删除角色失败!");
        }
        return result ;
    }

    @PutMapping("/authority")
    public ResultResponse<Void> authority(@RequestBody RoleVo roleVo){
        ResultResponse<Void> result = null;
        try {

            roleService.authority(roleVo);
            result = new ResultResponse<>(200,"授权成功!");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"授权失败!");
        }
        return result ;
    }


    @GetMapping("/showAllRole")
    public ResultResponse<List<Role>> showAllRole(@RequestParam(value = "now",defaultValue = "1") Integer now,
                                                  @RequestParam(value = "size",defaultValue = "3")Integer size,
                                                  String name){
        ResultResponse<List<Role>> result = null;
        try {
            List<Role> list = roleService.queryAllRole();
            result = new ResultResponse<>(200,"查询角色成功!",list);
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"查询角色失败!");
        }

        return result ;
    }
}
